﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Wicresoft.Busi.BizLogic;
using Wicresoft.Busi.Entity;

public partial class NewMeetingRoom_RoomApplyQuery : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 会议室名称";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "return searchClick();");

        if (!Page.IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            initRoomType();
            initRoom();
            initMeetingStatus();
            initMeetingType();
            this.Order.OrderColumn = vwMeetingRoomApply.SQLCOL_APPLYDATE;
            this.Order.OrderDirection = "DESC";
        }
    }

    #region 初始化会议室类型
    private void initMeetingType()
    {
        DataTable dt = BLDic_MeetingRoom.GetMeetingType();
        this.ddlType.DataSource = dt;
        this.ddlType.DataTextField = Dic_MeetingRoom.SQLCOL_PROPERTYNAME;
        this.ddlType.DataValueField = Dic_MeetingRoom.SQLCOL_PROPERTYID;
        this.ddlType.DataBind();
        ListItem item = new ListItem("- 所有 -", "-1");
        this.ddlType.Items.Insert(0, item);
    }
    #endregion

    private void initRoomType()
    {
        DataTable dt = BLDic_MeetingRoom.GetRoomType();
        this.ddlRoomType.DataSource = dt;
        this.ddlRoomType.DataTextField = Dic_MeetingRoom.SQLCOL_PROPERTYNAME;
        this.ddlRoomType.DataValueField = Dic_MeetingRoom.SQLCOL_PROPERTYID;
        this.ddlRoomType.DataBind();
        ListItem item = new ListItem("- 所有 -", "-1");
        this.ddlRoomType.Items.Insert(0, item);
    }

    private void initRoom()
    {
        DataTable dt = BLMeetingRooms.GetMeetingRooms(int.Parse(this.ddlRoomType.SelectedValue), CurrentUser.ID);
        dt.Columns.Add("RoomDesc", typeof(string));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            dr["RoomDesc"] = dr[vwMeetingRooms.SQLCOL_ROOMNAME].ToString() + " （座位数：" + dr[vwMeetingRooms.SQLCOL_SEATSCOUNT].ToString() + "人）";
        }
        this.ddlRoom.DataSource = dt;
        this.ddlRoom.DataTextField = "RoomDesc";
        this.ddlRoom.DataValueField = vwMeetingRooms.SQLCOL_ID;
        this.ddlRoom.DataBind();
        ListItem item = new ListItem("- 所有 -", "0");
        this.ddlRoom.Items.Insert(0, item);
    }

    private void initMeetingStatus()
    {
        DataTable dt = BLDic_MeetingRoom.GetMeetingStatus();
        this.ddlStatus.DataSource = dt;
        this.ddlStatus.DataTextField = Dic_MeetingRoom.SQLCOL_PROPERTYNAME;
        this.ddlStatus.DataValueField = Dic_MeetingRoom.SQLCOL_PROPERTYID;
        this.ddlStatus.DataBind();
        ListItem item = new ListItem("- 所有 -", "");
        this.ddlStatus.Items.Insert(0, item);
    }

    protected void ddlRoomType_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        initRoom();
        BindData();
    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        DateTime dtDateFrom, dtDateTo;
        try
        {
            dtDateFrom = DateTime.Parse(this.txtDateFrom.Text);
        }
        catch
        {
            dtDateFrom = DateTime.MinValue;
        }
        try
        {
            dtDateTo = DateTime.Parse(this.txtDateTo.Text);
            if (dtDateTo < dtDateFrom)
            {
                WebCommon.ShowMessage(this.Page, "截止时间应大于起始时间,请选择正确的时间.");
                return;
            }
        }
        catch
        {
            dtDateTo = DateTime.MinValue;
        }
        Wicresoft.Common.PagingEntity pe = new Wicresoft.Common.PagingEntity();
        if (this.Order.OrderDirection == "DESC")
        {
            pe.AscString = this.Order.OrderColumn +" "+ this.Order.OrderDirection;
            pe.DescString = this.Order.OrderColumn;
        }
        else
        {
            pe.AscString = this.Order.OrderColumn;
            pe.DescString = this.Order.OrderColumn + " " + this.Order.OrderDirection;
        }
        pe.PageIndex = this.Pager.PageIndex;
        pe.PageSize = this.Pager.PageSize ;
        pe.SelectString = "*";
        pe.TableName = "vwMeetingRoomApply2";
        DataTable dt = BLMeetingRoomApply.GetMeetingRoomApply(int.Parse(this.ddlRoomType.SelectedValue), int.Parse(this.ddlRoom.SelectedValue), int.Parse(this.ddlType.SelectedValue),
            this.txtRemark4.Text, this.ddlStatus.SelectedValue, SearchClause, this.txtRemark1.Text, dtDateFrom, dtDateTo, CurrentUser.ID, pe);
        foreach (DataRow dr in dt.Rows)
        {
            if (dr[Wicresoft.Busi.Entity.MeetingRoomApply.SQLCOL_TOPIC].ToString().Length > 10)
                dr[Wicresoft.Busi.Entity.MeetingRoomApply.SQLCOL_TOPIC] = dr[Wicresoft.Busi.Entity.MeetingRoomApply.SQLCOL_TOPIC].ToString().Substring(0, 10) + "...";
        }

        Pager.RecordCount = pe.RecordCount;
        this.rptApply.DataSource = dt;
        this.rptApply.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchClause = this.tbSearch.Text.Replace("'","");
        BindData();
    }

    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        SearchClause = this.txtTopic.Text.Replace("'", "");
        BindData();
    }

}
